﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entity.Model;
using SystemFrameWork;

namespace DutyWeb.UserControls
{
    public partial class Sys_RoleList : BaseUserControl
    {
        /// <summary>
        /// 
        /// </summary>
        private Sys_RoleManagement sys_RoleManagement = new Sys_RoleManagement();

        /// <summary>
        /// 
        /// </summary>
        private List<Guid> selectedRoles;

        /// <summary>
        /// 
        /// </summary>
        public List<Guid> SelectedRoles
        {
            get
            {
                if (selectedRoles == null)
                {
                    selectedRoles = new List<Guid>();
                }
                for (int i = 0; i < this.chkSys_RoleList.Items.Count; i++)
                {
                    if (chkSys_RoleList.Items[i].Selected)
                    {
                        this.selectedRoles.Add(new Guid(chkSys_RoleList.Items[i].Value));
                    }
                }
                return this.selectedRoles;
            }
            set
            {
                selectedRoles = value;
                if (selectedRoles.Count == 0)
                {
                    chkSys_RoleList.SelectedIndex = -1;
                    return;
                }
                foreach (Guid id in selectedRoles)
                {
                    for (int i = 0; i < this.chkSys_RoleList.Items.Count; i++)
                    {
                        if (chkSys_RoleList.Items[i].Value == id.ToString())
                        {
                            chkSys_RoleList.Items[i].Selected = true;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool BindedFlag
        {
            get
            {
                if (this.labBindedFlag.Text == ConstantsManager.True)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            set
            {
                this.labBindedFlag.Text = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindSys_RoleList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindSys_RoleList()
        {
            List<Sys_Role> list = this.sys_RoleManagement.LoadAllSys_Role();
            this.chkSys_RoleList.DataValueField = ConstantsManager.ID;
            this.chkSys_RoleList.DataTextField = ConstantsManager.Name;
            this.chkSys_RoleList.DataSource = list;
            this.chkSys_RoleList.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="enable"></param>
        public void EnableTheControl(bool enable)
        {
            chkSys_RoleList.Enabled = enable;
        }
    }
}